<?php

namespace app\command;

use support\spark\model\order\Order;
use support\spark\model\order\OrderOverdue;
use support\spark\model\user\UserBalanceRecharge;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;


class OrderTimer extends Command
{
    protected static $defaultName = 'sparks:orderTimer';
    protected static $defaultDescription = 'SparkShop商城各种订单超时定时器';


    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $overTimeOrderModel = new OrderOverdue();
        $overTimeOrderList = $overTimeOrderModel->select(['order_id', 'type'])->where([
            ['overdue_time', '<', now()]
        ])->get();
        $overTimeOrderList = $overTimeOrderList ? $overTimeOrderList->toArray() : [];
        if (!$overTimeOrderList) {
            return self::SUCCESS;
        }
        $orderModel = new Order();
        $balanceRechargeModel = new UserBalanceRecharge();
        foreach ($overTimeOrderList as $vo) {

            // 移除快检表的数据
            $overTimeOrderModel->delByWhere(['order_id' => $vo['order_id'], 'type' => $vo['type']]);
            switch ($vo['type']) {
                case 1:
                    $this->dealOrder($vo, $orderModel);
                    break;
                case 2:
                    $this->dealGroupBuyOrder($vo);
                    break;
                case 3:
                    $this->dealSeckillOrder($vo, $orderModel);
                    break;
                case 4:
                    $this->dealBargaining($vo);
                    break;
                case 5:
                    $this->dealBalanceOrder($vo, $balanceRechargeModel);
                    break;
            }
        }

        return self::SUCCESS;
    }

    /**
     * 处理普通订单
     * @param $orderInfo
     * @param Order $orderModel
     * @return array
     */
    protected function dealOrder($orderInfo, Order $orderModel)
    {
        return $orderModel->updateById([
            'status' => 8, // 已关闭
            'close_time' => now()
        ], $orderInfo['order_id']);
    }

    /**
     * 处理团购订单 TODO 待更新
     * @param $orderInfo
     * @return array
     */
    protected function dealGroupBuyOrder($orderInfo)
    {
        return dataReturn(0);
    }

    /**
     * 处理秒杀订单
     * @param $orderInfo
     * @param Order $orderModel
     * @return array
     */
    protected function dealSeckillOrder($orderInfo, Order $orderModel)
    {
        return $orderModel->updateById([
            'status' => 8, // 已关闭
            'close_time' => now()
        ], $orderInfo['order_id']);
    }

    /**
     * 处理砍价订单 TODO 待更新
     * @param $orderInfo
     * @return array
     */
    protected function dealBargaining($orderInfo)
    {
        return dataReturn(0);
    }

    /**
     * 处理余额充值订单
     * @param $orderInfo
     * @param UserBalanceRecharge $balanceRechargeModel
     * @return array
     */
    protected function dealBalanceOrder($orderInfo, UserBalanceRecharge $balanceRechargeModel)
    {
        return $balanceRechargeModel->updateById([
            'status' => 4
        ], $orderInfo['order_id']);
    }


}
